<?php
/**
 * Plugin Name: Event Loop
 * Description: Event Loop Widget.
 * Version: 1.0
 * Author: jonathan01
 * Author URI: http://themeforest.net/user/jonathan01 
 */


add_action( 'widgets_init', 'eventloop_widget' );


function eventloop_widget() {
	register_widget( 'EventLoop_Widget' );
}

class EventLoop_Widget extends WP_Widget {

	function EventLoop_Widget() {
		$widget_ops = array( 'classname' => 'example', 'description' => __('Use this to display an event loop in your sidebar of the last 4 events (by event date) ', 'genesis') );
		
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'example-widget' );
		
		$this->WP_Widget( 'example-widget', __('Sidebar Event Loop', 'genesis'), $widget_ops, $control_ops );
	}
	
	function widget( $args, $instance ) {
		extract( $args );

		//Our variables from the widget settings.
		$title = apply_filters('widget_title', $instance['title'] );
		$eventnumber = $instance['eventnumber'];

		echo $before_widget;

		// Display the widget title 
		if ( $title )
			echo $before_title . $title . $after_title;

		//Display the name 
		global $post; 
		$args = array(
		'post_type' => 'event',
		'posts_per_page' => $eventnumber,
		'order' => 'DESC',
		'orderby' => 'meta_value',
		'meta_key' => 'custom_date'
		);
		?>
	
		<?php query_posts( $args ); while ( have_posts() ) : the_post(); ?>
        
        <?php
        $date = get_post_meta($post->ID, 'custom_date', $single = true); 
        $eventtime = get_post_meta($post->ID, 'eventtime', $single = true); 
        $eventvenuename = get_post_meta($post->ID, 'eventvenuename', $single = true);  
        $eventaddress = get_post_meta($post->ID, 'eventaddress', $single = true);  
        $eventmaplink = get_post_meta($post->ID, 'eventmaplink', $single = true); 
        $eventmap = get_post_meta($post->ID, 'eventmap', $single = true);  
        $eventcost = get_post_meta($post->ID, 'eventcost', $single = true);   
        
        $extract_date = get_post_meta($post->ID, 'custom_date', $single = true); 
        ?>
        
        <!-- Start of sidebarevent -->
        <article class="sidebarevent" style="margin:20px 0 20px 0;">  
        
        <!-- Start of featured text full -->
        <div class="featured_text_nopad">
        <div class="eventmonth"><?php echo date("M", strtotime($extract_date)); ?></div>
        <div class="eventday"><?php echo date("d", strtotime($extract_date)); ?></div>
        
        </div><!-- End of featured text full -->
        
        <h6><a href="<?php the_permalink (); ?>"><?php the_title (); ?></a></h6>
        
        <!-- Start of event index -->
        <div class="event_index">
        
        <!-- Start of meta -->
        <div class="meta">
        
        <?php if ($eventtime != ('')){ ?> 
        <div class="time"></div>
        <span class="eventdeats"><?php echo ($eventtime); ?></span>
        
        <?php } ?>
        
        </div><!-- End of meta -->
        
        <!-- Start of meta -->
        <div class="meta">
        
        <?php if ($eventvenuename != ('')){ ?> 
        
        <div class="venue"></div>
        <span class="eventdeats"><?php echo ($eventvenuename); ?></span>
        
        <?php } ?>
        
        </div><!-- End of meta -->
        
        <!-- Start of clear fix --><div class="clear"></div>
        
        </div><!-- End of event index -->
        
        </article><!-- End of sidebarevent -->
                
        <?php endwhile; ?> 
        
        <div class="clear"></div>

	<?php
		
		echo $after_widget;
	}

	//Update the widget 
	 
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		//Strip tags from title and name to remove HTML 
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['eventnumber'] = strip_tags( $new_instance['eventnumber'] );

		return $instance;
	}

	
	function form( $instance ) {

		//Set up some default widget settings.
		$defaults = array( 'title' => __('Our Latest Events', 'genesis'));
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'genesis'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
		</p>
        
        <p>
			<label for="<?php echo $this->get_field_id( 'eventnumber' ); ?>"><?php _e('Number To Display:', 'genesis'); ?></label>
			<input id="<?php echo $this->get_field_id( 'eventnumber' ); ?>" name="<?php echo $this->get_field_name( 'eventnumber' ); ?>" value="<?php echo $instance['eventnumber']; ?>" style="width:20%;" />
		</p>

	<?php
	}
}

?>